﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using KTX.BLL;
using KTX.Public;

namespace KTX.Giaodien.Login
{
    public partial class FrmLogin : DevExpress.XtraEditors.XtraForm
    {
        public static int y = 0;
        ThanhvienBLL cls = new ThanhvienBLL();
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {            
            bsiCapslock.Caption = Console.CapsLock ? "Mở" : "Tắt";
            txtPass.Properties.UseSystemPasswordChar = true;
        }
                

        private void btnLogin_Click(object sender, EventArgs e)
        {
            int x = 1;
            do
            {
                var sss = new Session();
                sss = cls.CheckLogin(txtUser.Text.Trim(), txtPass.Text.Trim());
                //var sss = (new ThanhvienBLL()).CheckLogin(txtUser.Text, txtPass.Text);
                if (sss.Login)
                {
                    
                    
                    //FrmMain._sss = sss;
                    Close();
                    break;
                }
                else
                {
                    MessageBox.Show("Lỗi! Tài khoản hoặc mật khẩu không đúng", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    y = x + y;
                    FrmLogin_Load(sender, e);
                    break;
                }
            }
            while (y != 3);

            if (y == 3)
            {
                btnExit_Click(sender, e);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.CapsLock)
                bsiCapslock.Caption = Console.CapsLock ? "Mở" : "Tắt";
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin_Click(sender, e);
            if (e.KeyCode == Keys.CapsLock)
                bsiCapslock.Caption = Console.CapsLock ? "Mở" : "Tắt";
        }

        private void txtUser_Validating(object sender, CancelEventArgs e)
        {
            if (txtUser.Text + "" == "")
            {

                dxEP.SetError(txtUser, "Tài khoản không được trống");
            }
            else
                dxEP.SetError(txtUser, "");
        }

        private void txtPass_Validating(object sender, CancelEventArgs e)
        {
            if (txtPass.Text + "" == "")
                dxEP.SetError(txtPass, "Mật khẩu không được trống !!!");
            else
                dxEP.SetError(txtPass, "");
        }
    }
}